<template>
  <el-form ref="form" label-width="80px" class="login-form">
    <h2>欢迎</h2>
    <el-form-item label="用户名">
      <el-input v-model="username" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input type="password" v-model="password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item label="身份">
      <el-radio-group v-model="identity">
        <el-radio label="user">用户</el-radio>
        <el-radio label="business">商家</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">登录</el-button>
    </el-form-item>
  </el-form>
</template>
<style scoped>
.login-form {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
.login-form h2 {
  text-align: center;
  margin-bottom: 20px;
}
.login-form .el-form-item__label {
  text-align: left;
}
.login-form .el-form-item__content {
  margin-left: 80px;
}
</style>

<script>
import axios from "axios";
import qs from "qs";
import {setSessionStorage} from "@/common";
import router from "@/router";

export default {
  data() {
    return {
      username: '',
      password: '',
      identity: ''
    }
  },
  methods: {
    submitForm() {
      let url = ''
      if (this.identity === 'business') {
        url = 'business/login'
      } else if (this.identity === 'user') {
        url = 'user/login'
      }
      axios.post(url, qs.stringify({
        username: this.username,
        password: this.password
      })).then(response => {
        let user = response.data
        if (user === ''){
          alert('用户名或密码错误')
        }else {
          setSessionStorage('user',user);
          alert('登陆成功')
          if(user.username !== undefined){
            router.push({
              path: '/shopList'
            })
          }else{
            router.push({
              path: '/businessManage'
            })
          }
        }
      }).catch(error => {
        console.log(error)
      })
    }

  }
}
</script>
